﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using CrawlerCore;
using HtmlAgilityPack;

namespace WCrawlerLib
{
    public class BrowserSession_IPB32x : BrowserSession_IPB
    {
        public BrowserSession_IPB32x()
        {
            ForumTypeId = enumForumType.IPB32x;
        }

        ///// <summary>
        ///// Init params of POST  to login a forum.
        ///// </summary>
        //public string InitLoginParams(string postParams, params string[] pars)//(string username, string password)
        //{
        //    //Method 1
        //    _postParams = "auth_key=" + _htmlDoc.GetValueInInputTag("hidden", "name", "auth_key")
        //            + "&referer=" + pars[2]
        //            + "&ips_username=" + pars[0]
        //            + "&ips_password=" + pars[1]
        //            + "&rememberMe=" + "1";
        //    //set post paramaters
        //    return _postParams;
        //}

        /// <summary>
        /// Get threads with page number in paging
        /// </summary>
        /// <param name="forumUrl"></param>
        /// <param name="doc"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        public override List<DataRow> SearchThreadsByPaging(string forumUrl, HtmlDocument doc, DataTable table)
        {
            List<DataRow> rowList = new List<DataRow>();
            string xPath = @".//a[@title='View Topic']";
            HtmlNodeCollection nodeCollection = HtmlDoc.DocumentNode.SelectNodes(xPath);
            if (nodeCollection != null)
            {
                foreach (HtmlNode node in nodeCollection)
                {
                    string href = node.Attributes["href"].Value;
                    string title = node.InnerHtml;
                    DataRow row = table.NewRow();
                    //1. Get thread num
                    Regex r = new Regex(@"showtopic\=(?<threadnum>([0-9]+))");
                    Match m = r.Match(href);
                    if (m.Success)
                    {
                        row["ThreadNum"] = m.Result("${threadnum}");
                        r = new Regex(@"\&p\=(?<postnum>([0-9]+))");
                        m = r.Match(href);
                        if (m.Success)
                            row["PostNum"] = m.Result("${postnum}");
                        //2. Get title
                        row["Title"] = title;
                        row["Url"] = GeneralClass.GetFullUrl(forumUrl, href);
                        row["Replies"] = "0";
                        row["Views"] = "0";
                        rowList.Add(row);
                    }
                }
            }
            return rowList;
        }

        public override bool PostReply(string topicUrl, string forumUrl, string message)
        {
            throw new NotImplementedException();
        }

        public override bool PostNewReply(string forumUrl, string topicUrl, string newReplyUrl, string postParams, string message)
        {
            throw new NotImplementedException();
        }

        public override string SearchThreadsInTopic(string postTitle, string topicUrl, string searchTopic_xPath, string username)
        {
            throw new NotImplementedException();
        }
    }
}